<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs.*, core.*, util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<% String ROOT = request.getContextPath(); %>
<script src="<%=ROOT%>/res/se2/js/HuskyEZCreator.js" type="text/javascript"></script>
<script src="<%=ROOT%>/res/se2_init.js" type="text/javascript"></script>
<style>
    input[name=title] { width:700px; border-style:groove; margin:2px; }
    form select { margin-right: 100px; }
    textarea { width: 766px; height: 600px; display: none; }
    iframe { width: 100%;  border: none; }
</style>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int boardId = param.getInt("bid", 0);
    int articleId = param.getInt("aid", 0);
    String cmd = param.getString("cmd", "");
    String errMsg = null;

    Article article = ArticleDAO.selectById(articleId);
    BoardService boardService = new BoardService(request, boardId);
    if (boardService.canEditArticle(article) == false)
        new UnauthorizedAccessException("게시글 수정 권한 없음");

    String title = param.getString("title", article.getTitle());
    String body = param.getString("body", article.getBody());
    int category = param.getInt("category", article.getCategoryId());
    boolean notice = param.getBoolean("notice", false);
    String url = "article.jsp?" + request.getQueryString();

    if ("저장".equals(cmd)) {
        if (title.length() > 0) {
            if (body.length() > 0) {
                ArticleDAO.update(articleId, category, title, body, notice);
                response.sendRedirect(url);
                return;
            } else
                errMsg = "내용을 입력하세요";
        } else
            errMsg = "제목을 입력하세요";
    }
    Board board = BoardDAO.selectById(boardId);
    ArrayList<ArticleCategory> categories = ArticleCategoryDAO.selectAll(boardId);
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container">

    <h1><%= boardService.getBoard().getTitle() %></h1>
    <hr />

    <form method="post">
        <div>
            제목: <input type="text" name="title" value="<%= title %>" />
        </div>

        <div class="form-inline">
            분류:
            <select name="category">
                <% for (ArticleCategory ac : categories) { %>
                    <option value="<%= ac.getId() %>" <%= category == ac.getId() ? "selected" : "" %>>
                            <%= ac.getName() %></option>
                <% } %>
            </select>
            <label>
                공지: <input type="checkbox" name="notice" <%= notice ? "checked" : "" %> />
            </label>
        </div>

        <textarea id="articleBody" name="body" class="smarteditor2"><%= body %></textarea>

        <% if (boardService.getBoard().isFileAttachEnabled()) { %>
            <iframe src="fileUploadList.jsp?bid=<%= boardId %>&aid=<%= articleId %>">
            </iframe>
        <% } %>

        <button type="submit" class="btn btn-primary" name="cmd" value="저장">
                <i class="icon-hdd icon-white"></i> 저장</button>
        <a href="<%= url %>" class="btn">취소</a>
    </form>

    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
